using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

using Shine.Framework;

namespace Shine.Tool
{
	public class FrmBookMarkManage : Office2007Form
	{
		private IApplication m_Application = null;



		private IContainer components = null;

		private ButtonX btnNew;

		private ButtonX btnClose;

		private ButtonX btnRemoveAll;

		private ButtonX btnRemoveSelect;

		private ListViewEx lstvBookMark;

		private System.Windows.Forms.ColumnHeader bookmarkName;
        private ColumnHeader createDate;
        private ColumnHeader idx;
        private static FrmBookMarkManage mInstance;

        public IApplication Appliation
		{
			set
			{
				this.m_Application = value;
			}
		}
        public static FrmBookMarkManage Instance()
        {
            if (mInstance == null || mInstance.IsDisposed)
            {
                mInstance = new FrmBookMarkManage();
            }
            return mInstance;
        }
        private FrmBookMarkManage()
		{
			this.InitializeComponent();
		}

        public void LoadBookmarks()
        {
            try
            {
                lstvBookMark.Items.Clear();
                string xmlFile = Application.StartupPath + "\\bookmark.xml";

                if (System.IO.File.Exists(xmlFile))
                {
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.Load(xmlFile);
                    XmlNode root = xmlDoc.SelectSingleNode("bookmarks");
                    int idx = 0;
                    foreach (XmlNode node in root.ChildNodes)
                    {
                        idx++;
                        bm b = new Tool.bm();
                        b.Name = node.Attributes["Name"] == null ? "" : node.Attributes["Name"].Value;
                        b.CreateDate = node.Attributes["CreateDate"] == null ? "" : node.Attributes["CreateDate"].Value;
                        b.CenterPoint = node.Attributes["CenterPoint"] == null ? "" : node.Attributes["CenterPoint"].Value;
                        b.Altitude = node.Attributes["Altitude"] == null ? "" : node.Attributes["Altitude"].Value;
                        b.Heading = node.Attributes["Heading"] == null ? "" : node.Attributes["Heading"].Value;
                        b.Tilt = node.Attributes["Tilt"] == null ? "" : node.Attributes["Tilt"].Value;
                        ListViewItem item = lstvBookMark.Items.Add(idx.ToString());
                        item.SubItems.Add(b.Name);
                        item.SubItems.Add(b.CreateDate);
                        item.Tag = b;
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
        }
		private void FrmBookMarkManage_Load(object sender, System.EventArgs e)
		{
			if (this.m_Application != null)
			{
               
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			base.Close();
		}

		private void FrmBookMarkManage_Resize(object sender, System.EventArgs e)
		{
			if (base.Width < 233)
			{
				base.Width = 233;
			}
			if (base.Height < 317)
			{
				base.Height = 317;
			}
		}

		private void lstvBookMark_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			try
			{
				//if (this.lstvBookMark.SelectedItems != null && this.lstvBookMark.SelectedItems.Count == 1)
				//{
				//	System.Windows.Forms.ListViewItem lstvItem = this.lstvBookMark.SelectedItems[0];
				//	if (lstvItem.Tag != null && lstvItem.Tag is ISpatialBookmark)
				//	{
				//		ISpatialBookmark pSpatialBookmark = lstvItem.Tag as ISpatialBookmark;
				//		pSpatialBookmark.ZoomTo(this.m_pMap);
				//		((IActiveView)this.m_pMap).Refresh();
				//	}
				//}
			}
			catch (System.Exception ex)
			{
				
			}
		}

		private void btnZoomTo_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (this.lstvBookMark.SelectedItems.Count == 1)
				{
					//System.Windows.Forms.ListViewItem lstvItem = this.lstvBookMark.SelectedItems[0];
					//if (lstvItem.Tag != null && lstvItem.Tag is ISpatialBookmark)
					//{
					//	ISpatialBookmark pSpatialBookmark = lstvItem.Tag as ISpatialBookmark;
					//	pSpatialBookmark.ZoomTo(this.m_pMap);
					//	((IActiveView)this.m_pMap).Refresh();
					//}
				}
			}
			catch (System.Exception ex)
			{
				
			}
		}

	
	
	

	
		

		

        private void btnRemoveSelect_Click(object sender, System.EventArgs e)
        {
            if (this.lstvBookMark.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要删除的书签!", "提示", MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("确定要删除选择的书签吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                while(lstvBookMark.SelectedItems.Count>0)
                {
                    lstvBookMark.Items.Remove(lstvBookMark.SelectedItems[0]);
                }
                for(int i=0;i<lstvBookMark.Items.Count;i++)
                {
                    lstvBookMark.Items[i].Text = (i + 1).ToString();
                }
                SaveBookmarks();

            }


        }

        private void SaveBookmarks()
        {
            SuperMap.Realspace.Scene s = (m_Application as IFramework).SceneControl.Scene;
            string xmlFile = Application.StartupPath + "\\bookmark.xml";
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            if (System.IO.File.Exists(xmlFile) == false)
            {

                //doc.Save(xmlFile);
            }
            else
            {
                xmlDoc.Load(xmlFile);
            }
            XmlNode root = xmlDoc.SelectSingleNode("bookmarks");
            if (root == null)
            {
                root = xmlDoc.CreateElement("bookmarks");
                xmlDoc.AppendChild(root);
            }
            root.RemoveAll();
            foreach(ListViewItem item in lstvBookMark.Items)
            {
                bm b = item.Tag as bm;
                XmlElement xe1 = xmlDoc.CreateElement("bookmark");
                xe1.SetAttribute("Name", b.Name);
                xe1.SetAttribute("CreateDate", b.CreateDate);
                xe1.SetAttribute("CenterPoint", b.CenterPoint);
                xe1.SetAttribute("Altitude", b.Altitude);
                xe1.SetAttribute("Heading", b.Heading);
                xe1.SetAttribute("Tilt",b.Tilt); ;
                root.AppendChild(xe1);
            }
         
            xmlDoc.Save(xmlFile);

        }



        private void btnRemoveAll_Click(object sender, System.EventArgs e)
		{
            if (lstvBookMark.Items.Count == 0) return;
            if (MessageBox.Show("确定要清空书签吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                lstvBookMark.Items.Clear();
            }    SaveBookmarks();
            }

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			FrmAddBookMark frmInputBox = new FrmAddBookMark();
			frmInputBox.Appliation = this.m_Application;
            if (frmInputBox.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                SuperMap.Realspace.Scene s = (m_Application as IFramework).SceneControl.Scene;
                ListViewItem item = lstvBookMark.Items.Add((lstvBookMark.Items.Count + 1).ToString());
                item.SubItems.Add(frmInputBox.BookMarkerName);
                DateTime dt = DateTime.Now;
                item.SubItems.Add(dt.ToString());
                bm b = new bm();
                b.Name = frmInputBox.BookMarkerName;
                b.CreateDate = dt.ToString();
                b.CenterPoint = s.Camera.Longitude + "," + s.Camera.Latitude;
                b.Altitude = s.Camera.Altitude.ToString();
                b.Heading=s.Camera.Heading.ToString();
                b.Tilt = s.Camera.Tilt.ToString();
                item.Tag = b;
                
                SaveBookmarks();
            }
        }

	

	



		

	
	
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
            this.btnNew = new DevComponents.DotNetBar.ButtonX();
            this.btnClose = new DevComponents.DotNetBar.ButtonX();
            this.btnRemoveAll = new DevComponents.DotNetBar.ButtonX();
            this.btnRemoveSelect = new DevComponents.DotNetBar.ButtonX();
            this.lstvBookMark = new DevComponents.DotNetBar.Controls.ListViewEx();
            this.idx = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.bookmarkName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.createDate = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.SuspendLayout();
            // 
            // btnNew
            // 
            this.btnNew.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
            this.btnNew.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnNew.Location = new System.Drawing.Point(441, 12);
            this.btnNew.Name = "btnNew";
            this.btnNew.Size = new System.Drawing.Size(83, 30);
            this.btnNew.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
            this.btnNew.TabIndex = 12;
            this.btnNew.Text = "创建";
            this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
            // 
            // btnClose
            // 
            this.btnClose.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
            this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnClose.Location = new System.Drawing.Point(441, 153);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(83, 28);
            this.btnClose.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
            this.btnClose.TabIndex = 13;
            this.btnClose.Text = "关闭";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // btnRemoveAll
            // 
            this.btnRemoveAll.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
            this.btnRemoveAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnRemoveAll.Location = new System.Drawing.Point(441, 106);
            this.btnRemoveAll.Name = "btnRemoveAll";
            this.btnRemoveAll.Size = new System.Drawing.Size(84, 23);
            this.btnRemoveAll.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
            this.btnRemoveAll.TabIndex = 7;
            this.btnRemoveAll.Text = "清空";
            this.btnRemoveAll.Click += new System.EventHandler(this.btnRemoveAll_Click);
            // 
            // btnRemoveSelect
            // 
            this.btnRemoveSelect.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
            this.btnRemoveSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnRemoveSelect.Location = new System.Drawing.Point(440, 58);
            this.btnRemoveSelect.Name = "btnRemoveSelect";
            this.btnRemoveSelect.Size = new System.Drawing.Size(84, 23);
            this.btnRemoveSelect.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
            this.btnRemoveSelect.TabIndex = 6;
            this.btnRemoveSelect.Text = "删除";
            this.btnRemoveSelect.Click += new System.EventHandler(this.btnRemoveSelect_Click);
            // 
            // lstvBookMark
            // 
            this.lstvBookMark.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            // 
            // 
            // 
            this.lstvBookMark.Border.Class = "ListViewBorder";
            this.lstvBookMark.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.idx,
            this.bookmarkName,
            this.createDate});
            this.lstvBookMark.FullRowSelect = true;
            this.lstvBookMark.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            this.lstvBookMark.Location = new System.Drawing.Point(2, 5);
            this.lstvBookMark.Name = "lstvBookMark";
            this.lstvBookMark.Size = new System.Drawing.Size(433, 338);
            this.lstvBookMark.TabIndex = 1;
            this.lstvBookMark.UseCompatibleStateImageBehavior = false;
            this.lstvBookMark.View = System.Windows.Forms.View.Details;
            this.lstvBookMark.DoubleClick += new System.EventHandler(this.lstvBookMark_DoubleClick);
            this.lstvBookMark.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lstvBookMark_MouseDoubleClick);
            // 
            // idx
            // 
            this.idx.Text = "序号";
            this.idx.Width = 80;
            // 
            // bookmarkName
            // 
            this.bookmarkName.Text = "名称";
            this.bookmarkName.Width = 210;
            // 
            // createDate
            // 
            this.createDate.Text = "创建时间";
            this.createDate.Width = 135;
            // 
            // FrmBookMarkManage
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnClose;
            this.ClientSize = new System.Drawing.Size(528, 345);
            this.Controls.Add(this.lstvBookMark);
            this.Controls.Add(this.btnRemoveAll);
            this.Controls.Add(this.btnRemoveSelect);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnNew);
            this.DoubleBuffered = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmBookMarkManage";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "书签管理器";
            this.Load += new System.EventHandler(this.FrmBookMarkManage_Load);
            this.Resize += new System.EventHandler(this.FrmBookMarkManage_Resize);
            this.ResumeLayout(false);

		}

        private void lstvBookMark_DoubleClick(object sender, EventArgs e)
        {
            if(lstvBookMark.SelectedItems.Count==1)
            {
                bm b = lstvBookMark.SelectedItems[0].Tag as bm;
                SuperMap.Realspace.Scene s = (m_Application as IFramework).SceneControl.Scene;
                SuperMap.Realspace.Camera c = s.Camera;
                double altitude, heading, tlit;
              if( double.TryParse(b.Altitude,out altitude))
                {
                    c.Altitude = altitude;
                }
                if (double.TryParse(b.Heading, out heading))
                {
                    c.Heading = heading;
                }
                if (double.TryParse(b.Tilt, out tlit))
                {
                    c.Tilt = tlit;
                }
                if(string.IsNullOrEmpty(b.CenterPoint)==false )
                {
                    string[] strs = b.CenterPoint.Split(",".ToCharArray());
                    double lon, lat;
                    if(strs.Length==2)
                    {
                        if(double.TryParse(strs[0],out lon) & double.TryParse(strs[1],out lat))
                        {
                            c.Longitude = lon;
                            c.Latitude = lat;
                        }
                    }
                }
                s.Camera = c;

            }
        }
    }

    public class bm
    {
        public string Name { get; set; }
        public string CreateDate { get; set; }
        public string Heading { get; set; }
        public string CenterPoint { get; set; }
        public string Altitude { get; set; }
        public string Tilt { get; set; }

    }
}
